package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.UserInfoBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.GetUserContract;
import com.cgtong.android.mvp.model.GetUserModel;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import org.json.JSONObject;

public class GetUserPresenter implements GetUserContract.Presenter {
    private GetUserContract.View mView;
    private GetUserModel model;

    public GetUserPresenter(GetUserContract.View view) {
        this.mView = view;
        model = new GetUserModel();
    }

    @Override
    public void getUserInfo() {
        model.getUserInfo(new SimpleCallBack<UserInfoBean.UserInfoBeanObj>() {
            @Override
            public void onError(ApiException e) {

            }

            @Override
            public void onSuccess(UserInfoBean.UserInfoBeanObj userInfoBean) {
                mView.getUserInfo(userInfoBean);
            }
        });
    }

    @Override
    public void setUserInfo(String name, String nickname, String idcard, String avasterid, String phone, String sex,String location) {
        model.setUserInfo(name, nickname, idcard, avasterid, phone, sex,location, new SimpleCallBack<String>() {
            @Override
            public void onError(ApiException e) {
                mView.setUserInfoFail(e.getMessage());
            }

            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObj = new JSONObject(result);
                    if (jsonObj.optInt("code") == ConstantManager.RESULT_SUCCESS) {
                        mView.setUserInfoSuc("修改成功");
                    } else {
                        mView.setUserInfoFail(jsonObj.optString("msg"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}